package com.hyc.myvideo.utils.imguitl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hyc.myvideo.R;
import com.hyc.myvideo.widget.roundimageview.rounded.RoundedTransformationBuilder;
import com.hyc.utils.StringUtils;

import java.io.InputStream;

import static com.hyc.myvideo.AppContext.getPicasso;


public class ImgUitl {
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /***
     * 获取屏幕宽度
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context){
        int width = 0;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        width=size.x;
        return width;
    }


    /***
     * 网络图片
     * @param imgUrl url
     * @param imageView 图片控件
     * @param Width   宽
     * @param Height  高
     */
    public static void setImg(String imgUrl, ImageView imageView, int Width,int Height){
        getPicasso()
                .load(imgUrl)
                .resize(Width,Height)
                .error(R.mipmap.icon_my_head_not)
                .config(Bitmap.Config.RGB_565)
                .into(imageView);
    }
    public static void setImg(String imgUrl, ImageView imageView){
        getPicasso()
                .load(imgUrl)
                .error(R.mipmap.icon_my_head_not)
                .config(Bitmap.Config.RGB_565)
                .into(imageView);
    }

    /***
     * 加载头像 圆头图片
     * @param imgUrl 图片url
     * @param imageView 控件
     * @param targetWidthResId 设置大小
     */
  public static void setLocalRoundImg(String imgUrl, ImageView imageView, int targetWidthResId){
      if(StringUtils.isEmpty(imgUrl)){
          getPicasso()
                  .load(R.mipmap.icon_my_head_not)
                  .transform(new RoundedTransformationBuilder().cornerRadiusDp(5).oval(true).build())
                  .resizeDimen(targetWidthResId,targetWidthResId)
                  .error(R.mipmap.icon_my_head_not)
                  .config(Bitmap.Config.RGB_565)
                  .into(imageView);

      }else{
          getPicasso()
                  .load(imgUrl)
                  .transform(new RoundedTransformationBuilder().cornerRadiusDp(5).oval(true).build())
                  .resizeDimen(targetWidthResId,targetWidthResId)
                  .error(R.mipmap.icon_my_head_not)
                  .config(Bitmap.Config.RGB_565)
                  .into(imageView);
      }
  }



    /***
     * 获取屏幕高度
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        return display.getHeight();
    }

    /**
     * 读取图片文件
     */
    public static Bitmap CompressPicture(String pa) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(pa, opt);
        return bmp;
    }


}
